;;;Reformats bearing (DMS) text while optionally changing the angle
;;;and/or the direction.
;;;
;;;	AUTHOR: HENRY C. FRANCIS
;;;		425 N. ASHE ST.
;;;		SOUTHERN PINES, NC 28387
;;;		All rights reserved
;;;
(DEFUN C:NSANG ()
  (while
    (setq oldangent (entsel "\nSelect survey angle text to modify: "))
    (setq oldentdat (entget(car oldangent)))
    (if (and oldangent
	     (eq (cdr(assoc 0 oldentdat)) "TEXT")
	     (or
	       (wcmatch (cdr(assoc 1 oldentdat)) "*N*#*E*")
	       (wcmatch (cdr(assoc 1 oldentdat)) "*N*#*W*")
	       (wcmatch (cdr(assoc 1 oldentdat)) "*S*#*E*")
	       (wcmatch (cdr(assoc 1 oldentdat)) "*S*#*W*")
	     )
	)
      (progn
	(setq oldangstr (cdr(assoc 1 oldentdat)))
        (cond
	  ((wcmatch oldangstr "*N*#*E*")
	    (setq angtststr "N*#*E*"
		  nsdir "N"
		  ewdir "E"))
	  ((wcmatch oldangstr "*N*#*W*")
	    (setq angtststr "N*#*W*"
		  nsdir "N"
		  ewdir "W"))
	  ((wcmatch oldangstr "*S*#*E*")
	    (setq angtststr "S*#*E*"
		  nsdir "S"
		  ewdir "E"))
	  ((wcmatch oldangstr "*S*#*W*")
	    (setq angtststr "S*#*W*"
		  nsdir "S"
		  ewdir "W"))
	)
	(setq tstcnt 1)
	(while (not (wcmatch (substr oldangstr tstcnt) angtststr))
	  (setq tstcnt (1+ tstcnt))
	)
	(setq tststop 4)
	(while (not (wcmatch (substr oldangstr tstcnt tststop) (substr angtststr 1 (1-(strlen angtststr)))))
	  (setq tststop (1+ tststop))
	)
	(setq coreangstr (substr oldangstr (1+ tstcnt)(- tststop 2)))
	(if (wcmatch coreangstr " #*")
	  (setq coreangstr (substr coreangstr 2))
	)
	(if (wcmatch coreangstr "*# ")
	  (setq coreangstr (substr coreangstr (1-(strlen coreangstr))))
	)
	(if (> tstcnt 1)
	  (setq oldpretxt (substr oldangstr 1 (1- tstcnt)))
	  (setq oldpretxt "")
	)
	(if (< tststop (strlen oldangstr))
	  (setq oldpsttxt (substr oldangstr (+ tstcnt tststop)))
	  (setq oldpsttxt "")
	)
	(if
	  (wcmatch (substr coreangstr 1 1) "#")
	  (if
	    (wcmatch (substr coreangstr 1 2) "##")
	    (if
	      (wcmatch (substr coreangstr 1 3) "###")
	      (setq degstr (substr coreangstr 1 3)
		    degcnt 4)
	      (setq degstr (substr coreangstr 1 2)
		    degcnt 3)
	    )
	    (setq degstr (substr coreangstr 1 1)
		  degcnt 2)
	  )
	)
	(cond
	  ((eq (substr coreangstr degcnt 5) "%%221")
	  (setq degchr "%%221"
		coreangstr (strcat
			     (substr coreangstr 1 (1- degcnt))
			     "d"
			     (substr coreangstr (+ degcnt 5))
			   )
	  ))
	  ((eq (substr coreangstr degcnt 3) "%%d")
	  (setq degchr "%%d"
		coreangstr (strcat
			     (substr coreangstr 1 (1- degcnt))
			     "d"
			     (substr coreangstr (+ degcnt 3))
			   )
	  ))
	  ((eq (substr coreangstr degcnt 1)(chr 176))
	  (setq degchr (chr 176)
		coreangstr (strcat
			     (substr coreangstr 1 (1- degcnt))
			     "d"
			     (substr coreangstr (+ degcnt 1))
			   )
	  ))
	)
	(if (wcmatch coreangstr "*'*")
	  (progn
	    (setq mincnt nil)
	    (setq minloc (1+ degcnt))
	    (while (/=(substr coreangstr minloc 1)"'")
	      (if (wcmatch (substr coreangstr minloc 1) "#")
		(if mincnt nil
		  (setq mincnt minloc)
		)
	      )
	      (setq minloc (1+ minloc))
	    )
	    (setq minstr (substr coreangstr mincnt (- minloc mincnt)))
	  )
	)
	(if (wcmatch coreangstr "*\"*")
	  (progn
	    (setq seccnt nil)
	    (setq secloc (+ mincnt (strlen minstr)))
	    (while (/=(substr coreangstr secloc 1)"\"")
	      (if (wcmatch (substr coreangstr secloc 1) "#")
		(if seccnt nil
		  (setq seccnt secloc)
		)
	      )
	      (setq secloc (1+ secloc))
	    )
	    (setq secstr (substr coreangstr seccnt (- secloc seccnt)))
	  )
	)
	(setq newdeg (uint 1 "" "Change in degrees" (if newdeg newdeg nil)))
	(setq newmin (uint 1 "Omit" "Change in minutes" (if (and newmin(eq(type newmin)'INT)) newmin nil)))
	(setq newsec (uint 1 "Omit" "Change in seconds" (if (and newsec(eq(type newsec)'INT)) newsec nil)))
	(setq flipbrng (ukword 1 "Flip Keep" "Flip or Keep current bearing?" (if flipbrng flipbrng "Keep")))
	(if (eq flipbrng "Flip")
	  (progn
	    (if (eq nsdir "N")
	      (setq nsdir "S")
	      (setq nsdir "N")
	    )
	    (if (eq ewdir "E")
	      (setq ewdir "W")
	      (setq ewdir "E")
	    )
	  )
	)
	(if (= newsec "Omit")
	  (setq secstr "")
	  (if (= newsec 0)
	    (setq secint (atoi secstr)
		  secstr (strcat secstr "\""))
	    (cond ((and (< newsec 0)(>(abs newsec)(atoi secstr)))
	            (setq newmin (1- newmin)
			  secstr (strcat (itoa (+ 60 (atoi secstr) newsec))"\"")))
		  ((< newsec 0)
		    (setq secstr (strcat (itoa (+ (atoi secstr) newsec))"\"")))
		  ((> newsec 0)
		    (setq secint (+ (atoi secstr) newsec))
		    (if (>= secint 60)
		      (setq secstr (strcat (itoa (- secint 60))"\"")
			    newmin (1+ newmin))
		      (setq secstr (strcat (itoa secint)"\""))
		    ))
	    )
	  )
	)
	(if (= newmin "Omit")
	  (setq minstr "")
	  (if (= newmin 0)
	    (setq minint (atoi minstr)
		  minstr (strcat minstr "'"))
	    (cond ((and (< newmin 0)(>(abs newmin)(atoi minstr)))
	            (setq newdeg (1- newdeg)
			  minstr (strcat (itoa (+ 60 (atoi minstr) newmin))"'")))
		  ((< newmin 0)
		    (setq minstr (strcat (itoa (+ (atoi minstr) newmin))"'")))
		  ((> newmin 0)
		    (setq minint (+ (atoi minstr) newmin))
		    (if (>= minint 60)
		      (setq minstr (strcat (itoa (- minint 60))"'")
			    newdeg (1+ newdeg))
		      (setq minstr (strcat (itoa minint)"'"))
		    ))
	    )
	  )
	)
	  (setq degint (atoi degstr)
		degstr (strcat degstr degchr))
	  (cond ((and (< newdeg 0)(>(abs newdeg)degint))
	          (setq degstr (strcat (itoa (+ degint newdeg 90))degchr))
		      (if (/= newsec "Omit")
			(setq minstr (strcat (itoa (- 59 minint)) "'")
			      secstr (strcat (itoa (- 60 secint)) "\""))
			(setq minstr (strcat (itoa (- 60 minint)) "'"))
		      )
		      (if (eq ewdir "E")
		        (setq ewdir "W")
		        (setq ewdir "E")
		      )
		 )
		((< newdeg 0)
		  (if
		    (<(+ degint newdeg)0)
		    (progn
		      (setq degstr (strcat (itoa (+ 90 degint newdeg))degchr))
		      (if (/= newsec "Omit")
			(setq minstr (strcat (itoa (- 59 minint)) "'")
			      secstr (strcat (itoa (- 60 secint)) "\""))
			(setq minstr (strcat (itoa (- 60 minint)) "'"))
		      )
		      (if (eq ewdir "E")
		        (setq ewdir "W")
		        (setq ewdir "E")
		      )
		    )
		    (setq degstr (strcat (itoa (+ degint newdeg))degchr))
		  ))
		((> newdeg 0)
		  (setq degint (+ degint newdeg))
		  (if
		    (and (>= degint 90)(> minint 0)(> secint 0))
		    (progn
		      (setq degstr (strcat (itoa (- degint 90))degchr))
		      (if (/= newsec "Omit")
			(setq minstr (strcat (itoa (- 59 minint)) "'")
			      secstr (strcat (itoa (- 60 secint)) "\""))
			(setq minstr (strcat (itoa (- 60 minint)) "'"))
		      )
		      (if (eq nsdir "N")
		        (setq nsdir "S")
		        (setq nsdir "N")
		      )
		    )
		    (setq degstr (strcat (itoa degint)degchr))
		  ))
	  )
	(setq newangstr (strcat oldpretxt nsdir degstr
				(if(eq(strlen minstr)2)(strcat "0" minstr)minstr)
				(if(eq(strlen secstr)2)(strcat "0" secstr)secstr)
				ewdir oldpsttxt))
	(setq oldentdat
	       (subst (cons 1 newangstr)(assoc 1 oldentdat)oldentdat)
	)
	(setq oldentdat
	       (if (assoc 62 oldentdat)
	         (subst (cons 62 2)(assoc 62 oldentdat)oldentdat)
		 (append oldentdat (list(cons 62 2)))
	       )
	)
	(entmod oldentdat)
      )
      (princ "\nNo survey angle text selected")
    )
    (command ".undo" "mark")
  )
)